package com.roast.services.my;

import com.roast.util.QueryObject;
import java.util.ArrayList;
import com.roast.domain.*;

public class AddressToStarIMPL extends GeneralSO implements AddressToStar {

    private Event event;
    private Address address;
    private QueryObject queryObject;
    private DomainObject fromDB;

    @Override
    public int address(Event event) {
        return new AddressToStarIMPL().generalExecution(event);
    }

    @Override
    protected int executeSO(Object obj) {
        if (!setEvent(obj)) {
            return 10701;
        }
        if (!setAddress()) {
            return 10702;
        }
        setQueryObject();
        if (!readEvent()) {
            return 10703;
        }
        if (!addAddressToEvent()) {
            return 10704;
        }
        if (!copyObject(obj)) {
            return 10705;
        }
        return 10700;
    }

    private boolean setEvent(Object obj) {
        try {
            this.event = (Event) obj;
            return true;
        } catch (ClassCastException ex) {
            return false;
        }
    }

    private boolean setAddress() {
        ArrayList<Address> addressList;
        try {
            addressList = (ArrayList<Address>) event.getAddressList();
        } catch (ClassCastException ex) {
            return false;
        }

        if (addressList == null) {
            return false;
        }

        try {
            address = addressList.remove(0);
            return address != null;
        } catch (IndexOutOfBoundsException ex) {
            return false;
        }
    }

    private void setQueryObject() {
        queryObject = new QueryObject();
        queryObject.setDomaninObject(event);
    }

    private boolean readEvent() {
        fromDB = dbbr.readOneByID(queryObject);
        if (queryObject.getSignal() == 10020) {
            try {
                return true;
            } catch (ClassCastException ex) {
                return false;
            }
        }
        return false;
    }

    private boolean addAddressToEvent() {
        try {
            ((Event) fromDB).getAddressList().add(address);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    private boolean copyObject(Object obj) {
        try {
            return ((Event) obj).copyObject(fromDB);
        } catch (ClassCastException ex) {
            return false;
        }
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 10700:
                showOperationState(signal, "addressToStar: You have successfully address the star.");
                return true;
            case 10701:
                showOperationState(signal, "addressToStar:ERROR: Class cast exception.");
                return false;
            case 10702:
                showOperationState(signal, "addressToStar:ERROR: You fail to set address.");
                return false;
            case 10703:
                showOperationState(signal, "addressToStar:ERROR: You fail to read event from database.");
                return false;
            case 10704:
                showOperationState(signal, "addressToStar:ERROR: You fail to add address to event.");
                return false;
            case 10705:
                showOperationState(signal, "addressToStar:ERROR: Fail to copy object.");
                return false;
        }
        return false;
    }

}
